
import { defineStore } from 'pinia'
import action from '../../api/action'
export const useActionOrderStore = defineStore('actionOrder', {
    state: () => ({
        currentActionorder: {
            id: '',
            actionId: '',
            acTicketNumber: 0,
            acTicketPaidNumber: null,
            acTicketPrice: null,
        },
        actionName: '',
        actionDate: '',
        isOpen: false,
    }),
    actions: {
        setCurrentActionorder(data) {
            this.currentActionorder = data
        },
        // 设置当前操作名称
        setActionName(data) {
            this.actionName = data
        },
        // 设置活动时间
        setActionDate(data) {
            this.actionDate = data
        },
        // 设置是否公开
        setIsOpen(data) {
            this.isOpen = data
        },
        // 增加价格
        increasePrice() {
            if (this.currentActionorder.acTicketNumber <= 1000)
                this.currentActionorder.acTicketNumber += 10;
        },
        // 减价
        decreasePrice() {
            if (this.currentActionorder.acTicketNumber > 10)
                this.currentActionorder.acTicketNumber -= 10;
        },
        // 设置当前活动id
        setActionId(data) {
            this.currentActionorder.actionId = data
        },
        //获取活动订单信息
        loadCurrentActiion(){
            action.getActionOrderById(this.currentActionorder.actionId).then(res => { 
                console.log(res.data.data);
                //如果后端返回的数据为空，直接初始化
                if(res.data.data!=null){
                    this.setCurrentActionorder(res.data.data)
                }
            }).catch(err => { 
                console.log(err)
            })
        },
        //重置活动订单信息
        resetActionOrder(id) {
            this.currentActionorder = {
                id: '',
                actionId: '',
                acTicketNumber: 0,
                acTicketPaidNumber: null,
                acTicketPrice: null,
            }
            this.setIsOpen(false)
            this.setActionDate('')
            this.setActionName('')
        },
        //调用后端接口添加活动票信息
        addActionOrder() {
            action.addActionOrder(this.currentActionorder).then(res => {
                console.log(res)
            }).catch(err => {
                console.log(err)
            }).finally(() => {
                this.setIsOpen(false)
            })
        },
    },
    getters: {

    },
}
)